export default class CameraControllerScript extends Laya.Script3D {
    constructor() {
        super();
        this._up = new Laya.Vector3(0, 1, 0);
        this.position = new Laya.Vector3();
        this._tempVector3 = new Laya.Vector3();
        this.yawPitchRoll = new Laya.Vector3();
        this.tempRotation = new Laya.Quaternion();
        this.lerpRotation = new Laya.Quaternion();
        this.lerpPosition = new Laya.Vector3();
        this.sub = new Laya.Vector3();
        this.cameraPosition = new Laya.Vector3();
        this.targetPos = new Laya.Vector3();

        this.needDamping = true;

        //设置旋转弧度
        this.rotaionSpeed = 0.0006;
        //旋转值上限
        this.Yminlimit = -(Math.PI) / 4; this.Ymaxlimit = (Math.PI) / 8;
        this.leftMax = -(Math.PI * 2); this.rightMax = (Math.PI * 2);
        //限制距离
        this.minDistance = 5; this.maxDistance = 15;
        this.distance = 10;
        this.mSpeed = 0.3;

    }

    onAwake() {
        Laya.stage.on(Laya.Event.MOUSE_DOWN, this, this.mouseDown);
        Laya.stage.on(Laya.Event.MOUSE_UP, this, this.mouseUp);
        Laya.stage.on(Laya.Event.MOUSE_WHEEL, this, this.mWheel);

        //调整相机朝向模型
        this.set_target(this.owner);

    }

    _onDestroy() {
        //关闭监听函数
        Laya.stage.off(Laya.Event.RIGHT_MOUSE_DOWN, this, this.mouseDown);
        Laya.stage.off(Laya.Event.RIGHT_MOUSE_UP, this, this.mouseUp);
        Laya.stage.off(Laya.Event.MOUSE_WHEEL, this, this.mWheel);
    }

    set_target(mytarget) {
        //摄像机目标点朝向
        this.targetPos = mytarget.transform.position;
        this._camera = this.owner.scene.getChildByName("Main Camera");
        var camPos = this._camera.transform.position;
        this._camera.transform.lookAt(this.targetPos, this._up);

    }


    onUpdate() {

        var elapsedTime = Laya.timer.delta;

        if (!isNaN(this.lastMouseX) && !isNaN(this.lastMouseY) && this.isMouseDown) {
            var scene = this.owner.scene;

            var offsetX = Laya.stage.mouseX - this.lastMouseX;
            var offsetY = Laya.stage.mouseY - this.lastMouseY;


            var yprElem = this.yawPitchRoll;
            var x = offsetX * this.rotaionSpeed * elapsedTime;
            var y = offsetY * this.rotaionSpeed * elapsedTime;

            yprElem.x -= x;
            yprElem.y -= y;

            yprElem.x = this.clampAngle(yprElem.x, this.leftMax, this.rightMax);
            yprElem.y = this.clampAngle(yprElem.y, this.Yminlimit, this.Ymaxlimit);


            this.updateRotation();
        }
        this.lastMouseX = Laya.stage.mouseX;
        this.lastMouseY = Laya.stage.mouseY;



    }

    mouseDown(e) {
        //获得鼠标的旋转值
        this._camera.transform.localRotation.getYawPitchRoll(this.yawPitchRoll);
        //获得鼠标的xy值
        this.lastMouseX = Laya.stage.mouseX;
        this.lastMouseY = Laya.stage.mouseY;
        //设置bool值
        this.isMouseDown = true;

    }

    mWheel(e) {

        this.distance -= e.delta * this.mSpeed;
        this.updateRotation();
    }


    //控制相机的角度
    clampAngle(angle, min, max) {
        if (angle < -(Math.PI * 2)) {
            angle += (Math.PI * 2);
        }
        if (angle > (Math.PI * 2)) {
            angle -= (Math.PI * 2);

        }

        if (angle >= max) {
            angle = max;
        }

        if (angle <= min) {
            angle = min;
        }
        return angle;
    }


    //控制相机的距离
    clampDistance(distance, min, max) {

        if (distance >= max) {
            distance = max;
        }
        if (distance <= min) {
            distance = min;
        }
        return distance;

    }

    mouseUp(e) {
        //设置bool值
        this.isMouseDown = false;
    }

    updateRotation() {

        Laya.Quaternion.createFromYawPitchRoll(this.yawPitchRoll.x, this.yawPitchRoll.y, this.yawPitchRoll.z, this.tempRotation);

        //控制相机和物体距离
        this.distance = this.clampDistance(this.distance, this.minDistance, this.maxDistance);
        Laya.Vector3.transformQuat(new Laya.Vector3(0, 0, this.distance), this.tempRotation, this.position);
        Laya.Vector3.add(this.position, this.owner.transform.position, this.cameraPosition);


        // this._camera.transform.rotation=this.tempRotation; 
        // this._camera.transform.position=this.cameraPosition;
        

        //设置调整相机的角度和位置
         if (this.needDamping)
         {

            Laya.Quaternion.lerp(this._camera.transform.rotation,this.tempRotation,1, this.lerpRotation);
            Laya.Vector3.lerp(this._camera.transform.position, this.cameraPosition,1, this.lerpPosition);
            this._camera.transform.rotation = this.lerpRotation;
            this._camera.transform.position = this.lerpPosition;
         }
         else
         {
            this._camera.transform.rotation=this.tempRotation; 
            this._camera.transform.position=this.cameraPosition;
         }


    }
}